package com.yxq.mdt.app.modules.splash.adapter;

import android.view.View;
import android.view.ViewGroup;

import androidx.viewpager.widget.PagerAdapter;

import com.yxq.mdt.app.R;

import java.util.List;

public class GuideViewPageAdapter extends PagerAdapter {
    private List<View> viewList;
    private GuideViewPageAdapterInterface guideViewPageAdapterInterface;

    public void setGuideViewPageAdapterInterface(GuideViewPageAdapterInterface guideViewPageAdapterInterface) {
        this.guideViewPageAdapterInterface = guideViewPageAdapterInterface;
    }

    public interface GuideViewPageAdapterInterface {
        void start();
    }

    public GuideViewPageAdapter(List<View> viewList) {
        this.viewList = viewList;
    }

    @Override
    public int getCount() {
        return viewList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = viewList.get(position);
        container.addView(view);
        if (position == 3) {
            view.findViewById(R.id.btn_in_app).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (guideViewPageAdapterInterface != null) {
                        guideViewPageAdapterInterface.start();
                    }
                }
            });
        }
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}